Module modUnexpectedExceptionHandler

    Public g_ExceptionLogFileName As String = System.AppDomain.CurrentDomain.BaseDirectory & "CBuilder.log"

    Friend Sub HandleException(ByVal strModule As String, ByVal e As Exception)

        'purpose: to provide logging information on errors that are NOT expected... ie handled with 'Catch ex as Exception'
        '             instead of 'Catch ex as System.Data.DataException' and so forth. These are UNEXPECTED errors. :P

        Dim strMessage As String
        Dim strCaption As String

        Try
            strMessage = "Timestamp: " & Now() & vbCrLf & _
                                "Exception: " & e.Message & vbCrLf & _
                               "Module: " & strModule & vbCrLf & _
                               "Method: " & e.TargetSite.Name & vbCrLf

            ' build the title bar.
            With System.Reflection.Assembly.GetExecutingAssembly.GetName.Version
                strCaption = "Unexpected Exception. Version " & _
                                 .Major & "." & .Minor & Format(.Revision, "0000")
            End With

            ' open streamwriter to save, or append to log file. The log text has the inclusion of the stack trace.
            Dim objStream As New System.IO.StreamWriter(g_ExceptionLogFileName, True)
            Dim logText As String = strMessage & "Stack: " & e.StackTrace & vbCrLf

            ' write yonder message to da stream
            objStream.WriteLine(logText)
            ' send it away to da file and then close
            objStream.Flush()
            objStream.Close()

            ' show the error to yonder user with a perty message that this is a totally unexpected exception
            MessageBox.Show(strMessage, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally

        End Try

    End Sub

End Module
